﻿Imports System.Collections.Generic
Imports System.Data.SqlClient
' Student class
'
' This class provides get and set methods for IdNumber, Password,
' Program, PClassTime. 
' This class inherits from class Person. 
'
' By Taji Abdullah 6/30/08

Public Class Student

    Inherits Person       ' Base class

    'Class variables
    Private studentNumber As Integer        ' To hold the student id number
    Private sPassword As String              ' To hold the students password
    Private sProgram As String               ' To hold the students program enrolled in
    Private perferClassTime As String       ' To hold the students perfered class time
    'Private a As Hashtable(Of String, String)
    Private a As System.Collections.Generic.SortedList(Of String, Integer)
    Private courses As ArrayList
    Dim CN As New SqlConnection

    ' Constructor
    Public Sub New()
        Debug.WriteLine("Student object being created.")
        studentNumber = 0
        sPassword = " "
        sProgram = " "
        perferClassTime = " "
    End Sub

    ' IdNumber get and set methods.
    Public Property IdNumber() As Integer
        Get
            'a.Item("IT308") += 1

            Return studentNumber
        End Get
        Set(ByVal value As Integer)
            studentNumber = value
        End Set
    End Property

    ' Password get and set methods.
    Public Property Password() As String
        Get
            Return sPassword
        End Get
        Set(ByVal value As String)
            sPassword = value
        End Set
    End Property

    ' Program get and set methods.
    Public Property Program() As String
        Get
            Return sProgram
        End Get
        Set(ByVal value As String)
            sProgram = value
        End Set
    End Property

    ' PClassTime get and set methods.
    Public Property PClassTime() As String
        Get
            Return perferClassTime
        End Get
        Set(ByVal value As String)
            perferClassTime = value
        End Set
    End Property

    ' Destructor
    Public Overloads Sub Clear()
        studentNumber = 0
        sPassword = " "
        sProgram = " "
        perferClassTime = " "
    End Sub
    Public Function getCourseHours() As Integer
        'WB - Creates the connection and query statement
        CN.ConnectionString = My.Settings.IT308ConnectionString
        Dim cmd As New SqlCommand
        cmd.CommandText = "SELECT sum(COURSES.CreditHours) as CreditHours FROM COURSES inner join STUDENTSCOURSES on COURSES.ID = STUDENTSCOURSES.CoursesID WHERE STUDENTSCOURSES.StudentID = @StudentID and STUDENTSCOURSES.Grade > 59"
        Dim Reader As SqlDataReader = Nothing
        cmd.Parameters.AddWithValue("@StudentID", IdNumber)
        Dim intCourseHours As Integer = 0
        'WB - tries to open the connection and reports error if fails.
        Try
            CN.Open()
        Catch ex As Exception
            MessageBox.Show("Could not get courses for " & IdNumber & " - " & ex.Message)
        End Try
        'WB - reads in all of the course hours the student has completed.
        Try
            Reader = cmd.ExecuteReader
            While Reader.Read
                intCourseHours = Convert.ToInt32(Reader.Item("CreditHours"))
            End While
        Catch ex As Exception
            MessageBox.Show("Could not get course hours for " & IdNumber & " - " & ex.Message)
        Finally
            CN.Close()
            Reader.Close()
        End Try
        'WB - returns the number of course hours the student has completed
        Return intCourseHours

    End Function


    Public Function getPossibleCourses() As ArrayList
        'WB - creates the connection and query statement.
        CN.ConnectionString = My.Settings.IT308ConnectionString
        Dim cmd As New SqlCommand
        courses = New ArrayList
        cmd.Connection = CN
        cmd.CommandText = "getStudentsCourses"
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddWithValue("@StudentID", IdNumber)
        Dim Reader As SqlDataReader = Nothing
        Dim strCourseID As String = ""

        'WB - tries to open the connection and reports error if fails.
        Try
            CN.Open()
        Catch ex As Exception
            MessageBox.Show("Could not get courses for " & IdNumber & " - " & ex.Message)
        End Try
        'WB - clears the courses array list
        courses.Clear()

        'WB - reads in all of the courses the student has not taken yet.
        Try
            Reader = cmd.ExecuteReader
            While Reader.Read
                courses.Add(Convert.ToString(Reader.Item("CoursesID")))
            End While
        Catch ex As Exception
            MessageBox.Show("Could not get courses for " & IdNumber & " - " & ex.Message)
        Finally
            CN.Close()
            Reader.Close()
        End Try
        'WB - Returns the courses the student has not taken
        Return courses
    End Function

End Class
